﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Data;

namespace SMS.ViewModel.Master
{
    public class SupplierMasterViewModel : BaseNotify
    {
        SMS.Model.SupplierModel _model;

        public SupplierMasterViewModel()
        {
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);
            NewCommand = new DelegateCommand(New_Click, null);
            CancelCommand = new DelegateCommand(Cancel, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.SupplierModel();
                _model.OnServiceError += new Action<Entities.SMSException>(_model_OnServiceError);
                _model.OnSaveSupplierCompleted += new Action<int>(_model_OnSaveSupplierCompleted);
                _model.OnSearchCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.Supplier>>(_model_OnSearchCompleted);
                _model.OnGetCompleteSupplier += new Action<Entities.Supplier>(_model_OnGetCompleteSupplier);
            }
        }

        void _model_OnGetCompleteSupplier(Entities.Supplier obj)
        {
            CompleteSupplier = obj;
        }

        void _model_OnSaveSupplierCompleted(int obj)
        {
            UserMessage = "Supplier saved successfully.";
            CompleteSupplier.Supplier_ID = obj;
            SelectedMaster.Supplier_ID = obj;
        }

        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.Supplier> obj)
        {
            if (obj != null)
            {
                UserMessage = obj.Count + " Supplier found";
                SupplierList = new List<Entities.Supplier>(obj);
                PagedSupplierList = new PagedCollectionView(SupplierList);
            }
        }

        void _model_OnServiceError(Entities.SMSException obj)
        {
            ErrorMessage = obj.Message;
        }


        private List<Entities.Supplier> _SupplierList;

        public List<Entities.Supplier> SupplierList
        {
            get { return _SupplierList; }
            set { _SupplierList = value; OnPropertyChanged("SupplierList"); }
        }
       

        private Entities.City _SelectedCity;

        public Entities.City SelectedCity
        {
            get { return _SelectedCity; }
            set { _SelectedCity = value; OnPropertyChanged("SelectedCity"); }
        }

        private bool IsLoadComplete = true;

        private Entities.Supplier _SelectedMaster;

        public Entities.Supplier SelectedMaster
        {
            get { return _SelectedMaster; }
            set
            {
                _SelectedMaster = value;
                OnPropertyChanged("SelectedMaster");
                if (IsLoadComplete && value != null)
                {
                    if (value.Supplier_ID > 0)
                    {
                        _model.GetCompleteSupplier(value.Supplier_ID);
                        UserMessage = "Getting complete Supplier...";
                    }
                    else
                    {
                        CompleteSupplier = value;
                    }
                }
            }
        }

        private Entities.Supplier _CompleteSupplier;

        public Entities.Supplier CompleteSupplier
        {
            get { return _CompleteSupplier; }
            set { _CompleteSupplier = value; OnPropertyChanged("CompleteSupplier"); }
        }


        private PagedCollectionView _PagedSupplierList;

        public PagedCollectionView PagedSupplierList
        {
            get { return _PagedSupplierList; }
            set { _PagedSupplierList = value; OnPropertyChanged("PagedSupplierList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string _Text;

        public string Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }


        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {
            _model.SearchSupplier(SearchText);

            UserMessage = "Searching Supplier...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (CompleteSupplier != null)
            {
                _model.SaveSupplier(CompleteSupplier);

                UserMessage = "Saving Supplier...";
            }
            else
            {
                ErrorMessage = "No Supplier to save";
            }
        }

        public ICommand NewCommand { get; set; }

        public void New_Click(object param)
        {
            IsLoadComplete = false;
            if (SupplierList == null)
                SupplierList = new List<Entities.Supplier>();
            var k = new Entities.Supplier() { IsLive = true };
            SupplierList.Add(k);
            PagedSupplierList = new PagedCollectionView(SupplierList);
            PagedSupplierList.MoveToLastPage();
            SelectedMaster = k;
            IsLoadComplete = true;
        }

        public ICommand CancelCommand { get; set; }
        public void Cancel(object param)
        {
            ViewModel_MainPage.MainPageContext.FrameControl.GoBack();
        }
    }
}

